#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/3/20 10:16 PM
# @Author  : Hardy
# @File    : get_all_poi.py

import requests
import json
import pandas as pd
import sys
import time
time.sleep(0.001)

KEY = '3bce136bd311f4cc6572730a1581fe95'

URL = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=%s&key=3bce136bd311f4cc6572730a1581fe95&radius=50&extensions=all'


if __name__ == '__main__':
    # location = '114.08037299,30.316278754'
    # in_file = '/Users/hardy/app/0-car_geographic/data/user_session_split.csv'
    # out_file = '/Users/hardy/data/inv_loc.tsv'
    in_file = sys.argv[1]
    out_file = sys.argv[2]
    # out = requests.get(URL % (location,))
    # a = json.dumps(out.json(), ensure_ascii=False)

    df = pd.read_csv(in_file)
    out_fd = open(out_file, 'w+')
    for i, k in enumerate(zip(df['user_id'], df['session_id'], df['start_lon'], df['start_lat'], df['start_geohash'], df['end_lon'], df['end_lat'], df['end_geohash'])):
        if 994 < i < 150000:
            k = [str(m) for m in k]
            start_loc = str(k[2]) + ',' + str(k[3])
            end_loc = str(k[5]) + ',' + str(k[6])
            time.sleep(0.002)
            start_loc_out = requests.get(URL % (start_loc,))
            start_loc_poi = json.dumps(start_loc_out.json(), ensure_ascii=False)
            # print(start_loc_poi)
            time.sleep(0.002)
            end_loc_out = requests.get(URL % (end_loc,))
            end_loc_poi = json.dumps(end_loc_out.json(), ensure_ascii=False)
            # print(end_loc_poi)

            out = '\t'.join(k + [start_loc_poi, end_loc_poi])
            print(out)
            out_fd.write(out + '\n')
    out_fd.close()








